package desmontec.maquinas


import desmontec.sistema.SecUser
import desmontec.alertas.AlertaMaquinaService;
import desmontec.aluguel.AluguelMaquina

class MaquinaController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def springSecurityService
	AlertaMaquinaService alertaMaquinaService
	
    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        def maquinasUsuario = Maquina.findAllByUsuario((SecUser) springSecurityService.getCurrentUser(),params)
        [maquinaInstanceList: maquinasUsuario, maquinaInstanceTotal: Maquina.findAllByUsuario((SecUser) springSecurityService.getCurrentUser()).size()]
    }

    def create = {
        def maquinaInstance = new Maquina()
        maquinaInstance.properties = params
		maquinaInstance.usuario = (SecUser) springSecurityService.getCurrentUser()
        return [maquinaInstance: maquinaInstance]
    }

    def save = {
        def maquinaInstance = new Maquina(params)
		maquinaInstance.usuario = (SecUser) springSecurityService.getCurrentUser()
		if (maquinaInstance.save(flush: true)) {
            flash.message = "${message(code: 'desmontec.maquinas.Maquina.default.created.message', args: [message(code: 'maquina.label', default: 'Maquina'), maquinaInstance.id])}"
            redirect(action: "show", id: maquinaInstance.id)
        }
        else {
            render(view: "create", model: [maquinaInstance: maquinaInstance])
        }
    }

    def show = {
        def maquinaInstance = Maquina.get(params.id)
        def listaAlugueis = AluguelMaquina.findAllByMaquina(maquinaInstance, [sort:"inicioValidade", order:"desc"])
        if (!maquinaInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'maquina.label', default: 'Maquina'), params.id])}"
            redirect(action: "list")
        }
        else {
        	[maquinaInstance: maquinaInstance, listaAlugueis:listaAlugueis]
        }
    }

    def edit = {
        def maquinaInstance = Maquina.get(params.id)
        if (!maquinaInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'maquina.label', default: 'Maquina'), params.id])}"
            redirect(action: "list")
        }
        else {
        	def listaAlugueis = AluguelMaquina.findAllByMaquina(maquinaInstance, [sort:"inicioValidade", order:"desc"])
            return [maquinaInstance: maquinaInstance, listaAlugueis:listaAlugueis]
        }
    }

    def update = {
        def maquinaInstance = Maquina.get(params.id)
		
		if (maquinaInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (maquinaInstance.version > version) {
                    
                    maquinaInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'maquina.label', default: 'Maquina')] as Object[], "Another user has updated this Maquina while you were editing")
                    render(view: "edit", model: [maquinaInstance: maquinaInstance])
                    return
                }
            }
            maquinaInstance.properties = params
            if (!maquinaInstance.hasErrors() && maquinaInstance.save(flush: true)) {
				//fernando.alves - inicio
				alertaMaquinaService.atualizaAlertasManutencao(maquinaInstance)
				//fernando.alves - fim
				flash.message = "${message(code: 'desmontec.maquinas.Maquina.default.updated.message', args: [message(code: 'maquina.label', default: 'Maquina'), maquinaInstance.id])}"
                redirect(action: "show", id: maquinaInstance.id)
            }
            else {
                render(view: "edit", model: [maquinaInstance: maquinaInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'maquina.label', default: 'Maquina'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def maquinaInstance = Maquina.get(params.id)
        if (maquinaInstance) {
            try {
                maquinaInstance.delete(flush: true)
                flash.message = "${message(code: 'desmontec.maquinas.Maquina.default.deleted.message', args: [message(code: 'maquina.label', default: 'Maquina'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'maquina.label', default: 'Maquina'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'maquina.label', default: 'Maquina'), params.id])}"
            redirect(action: "list")
        }
    }
	
	def atualizarDataManutencao = {
		def maquinaInstance = Maquina.get(params.id)
		render (view: "atualizarDataManutencao", model: [maquinaInstance: maquinaInstance])
	}
	
}
